const JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = 'secret';

const mongoose = require("mongoose")
const User = mongoose.model('User')

module.exports = passport => {
    passport.use(new JwtStrategy(opts, async (jwt_payload, done) => {
        // console.log(jwt_payload)
        User.findOne({_id: jwt_payload.id}, (err, user) => {
            if (err) {
                return done(err, false);
            }
            if (user) {
                return done(null, user);
            } else {
                return done(null, false);
            }
        });
    }))
}